 package com.ee.bruscar.service.simple;

import java.util.Map;
import java.util.HashMap;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;

/**
 * This class is automatically generated.
 */
public class SimpleCustomerServiceStub implements com.ee.bruscar.service.simple.SimpleCustomerService
{
	private Map<String,String> hintMap;
	private DataMaker maker;
	
	public SimpleCustomerServiceStub()
	{
		maker = new DataMaker();
		
		hintMap = new HashMap<String, String>();
		hintMap.put( "SimpleCustomer.name", "Ed,Marty,Ralf,Ignacio,Andrei" );
		hintMap.put( "SimpleAccountDetails.accountId", "11111,22223,45678,34567" );
		hintMap.put( "SimpleAccountDetails.accountName", "Mr.Ed Eustace,Mr Ralf Bokelberg, Mr Ignacio Martin, Mr Andrei Jurescu" );
		hintMap.put( "SimpleGameCoupon.name", "LOTTO,ODDSET,NETSPIL,BINGO,ZEZAM" );
		hintMap.put( "SimpleGameCoupon.amountWon", "1.000kr,2.000kr,3.000kr,4.000kr,5.000kr,6.000kr" );
	}
	
	/**
	 * Interface methods
	 */
	public com.ee.bruscar.model.dto.simple.SimpleCustomer getCustomer( java.lang.String arg1, int arg2 )
	{
		return createStubSimpleCustomer();
	} 

	public List<SimpleGameCoupon> getGameCoupons( java.lang.String arg1 )
	{
		List<SimpleGameCoupon> returnList = new ArrayList<SimpleGameCoupon>();
		for( int i = 0 ; i < 10 ; i++ )
		{
			returnList.add( createStubSimpleGameCoupon() );
		}
		return returnList;
	} 
	
	/**
	 * custom class generators
	 */
	private com.ee.bruscar.model.dto.simple.SimpleAddress createStubSimpleAddress()
	{
		com.ee.bruscar.model.dto.simple.SimpleAddress stub = new com.ee.bruscar.model.dto.simple.SimpleAddress();
		stub.setHouseNumber( (String) getStubFor( "SimpleAddress.houseNumber", String.class ) );
		stub.setStreet( (String) getStubFor( "SimpleAddress.street", String.class ) );
		stub.setPostcode( (String) getStubFor( "SimpleAddress.postcode", String.class ) );
		stub.setCity( (String) getStubFor( "SimpleAddress.city", String.class ) );
		stub.setCountry( (String) getStubFor( "SimpleAddress.country", String.class ) );
		return stub;
	}


	private com.ee.bruscar.model.dto.simple.SimpleAccountDetails createStubSimpleAccountDetails()
	{
		com.ee.bruscar.model.dto.simple.SimpleAccountDetails stub = new com.ee.bruscar.model.dto.simple.SimpleAccountDetails();
		stub.setDateOpened( (Date) getStubFor( "SimpleAccountDetails.dateOpened", Date.class ) );
		stub.setAccountId( (String) getStubFor( "SimpleAccountDetails.accountId", String.class ) );
		stub.setAccountName( (String) getStubFor( "SimpleAccountDetails.accountName", String.class ) );
		return stub;
	}


	private com.ee.bruscar.service.simple.SimpleGameCoupon createStubSimpleGameCoupon()
	{
		com.ee.bruscar.service.simple.SimpleGameCoupon stub = new com.ee.bruscar.service.simple.SimpleGameCoupon();
		stub.setAmountWon( (String) getStubFor( "SimpleGameCoupon.amountWon", String.class ) );
		stub.setName( (String) getStubFor( "SimpleGameCoupon.name", String.class ) );
		return stub;
	}


	private com.ee.bruscar.model.dto.simple.SimpleCustomer createStubSimpleCustomer()
	{
		com.ee.bruscar.model.dto.simple.SimpleCustomer stub = new com.ee.bruscar.model.dto.simple.SimpleCustomer();
		stub.setDetails( createStubSimpleAccountDetails() );
		stub.setName( (String) getStubFor( "SimpleCustomer.name", String.class ) );
		stub.setAddress( createStubSimpleAddress() );
		return stub;
	}

	
	/**
	 * non dynamic stubbing logic
	 */
	private Object getStubFor( String fieldName, Class typeExpected )
	{
		if( hintMap.containsKey(fieldName) )
		{
			return maker.createData( hintMap.get( fieldName ), typeExpected );
		}
			
		return getGenericValue( fieldName, typeExpected );
	}

	private Object getGenericValue( String fieldName, Class typeExpected )
	{
		if( typeExpected == String.class )
		{
			return fieldName;
		}
		if( typeExpected == Date.class )
		{
			return new Date();
		}
		return null;
	}

	public class DataMaker
	{
		private Map<Class, Maker> makerMap;
		
		public DataMaker()
		{
			makerMap = new HashMap<Class, Maker>();
			makerMap.put(String.class, new StringMaker() );
			makerMap.put(Date.class, new DateMaker() );
		}
		
		public Object createData(String hint, Class typeExpected )
		{
			Maker maker = makerMap.get( typeExpected );
			if( maker == null )
			{
				throw new RuntimeException("Couldn't find maker for: " + typeExpected.getName() );
			}
			return maker.createData(hint);
		}

		public abstract class Maker
		{
			public abstract Object createData( String hint );
		}
		
		private class StringMaker extends Maker
		{
			public String createData(String hint )
			{
				String[] items = hint.split(",");
				int index = getRandomIndex( items.length );
				return items[index];
			}

			private int getRandomIndex(int length)
			{
				double random = Math.random();
				double multiplied = random * length;
				
				Double dbl = new Double( multiplied );
				return dbl.intValue();
			}
		}
			
		private class DateMaker extends Maker
		{
			public Date createData( String hint )
			{
				return new Date();
			}
		}
	}

}